package com.be.beadmin.service.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *  任务执行异常捕获<br>
 *  提交的任务数 > 排队队列深度 + 最大线程数时触发
 *  @author xunzhang
 *  @Date 2020.08.30 13:32
 */
public class TaskRejectedHandler implements RejectedExecutionHandler {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * @see RejectedExecutionHandler
	 * #rejectedExecution(java.lang.Runnable, java.util.concurrent.ThreadPoolExecutor)
	 */
	@Override
	public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
		logger.error("任务线程池执行异常！");
		logger.error(
				"线程池核心线程数：[ {} ]，线程池当前活动任务数：[ {} ]，线程池最大线程数：[ {} ]", 
				executor.getCorePoolSize(), 
				executor.getActiveCount(), 
				executor.getMaximumPoolSize());
		if (r instanceof Task) {
			Task task = (Task) r;
			logger.error("任务[ {} ]未执行", task.getName());
		}
	}
}
